<?php

namespace App\Admin\Forms\Config;

use App\Models\Config as Model;
use Dcat\Admin\Widgets\Dropdown;
use Dcat\Admin\Widgets\Form;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class Config extends Form
{
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
         $res = 0;
         if ($input){
             try {
                 foreach ($input as $key=>$value){
                     $model = Model::where('name',$key)->first();
                     $model->value = $value;
                     $res += $model->save();
                 }
             }catch (ModelNotFoundException|Exception $e){
                 return $this->response()->error($e->getMessage())->refresh();
             }
         }
         return $this->response()->success($res?'保存成功.':'暂无修改.')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $groupList = Model::getGroupList();
        foreach ($groupList as $field=>$tabName){
            $this->tab($tabName, function () use ($field) {
                $list = Model::where('group',$field)->where("module","admin")->get();
                foreach ($list as $value){
                    $this->column(6,function () use ($value) {
                        if ($value['type']!='array'){
                            $fieldModel = $this->{$value['type']}($value['name'],$value['title']);
                            if ($value['content']&&is_string($value['content'])){
                                $options = json_decode($value['content'],true);
                                $fieldModel->options($options);
                            }
                            if ($value['value']){
                                $fieldModel->value($value['value'])->default($value['value']);
                            }
                            if ($value['required']){
                                $fieldModel->required();
                            }
                            if ($value['tip']){
                                $fieldModel->help($value['tip']);
                            }
                            if (env('APP_DEBUG')){
                                $editUrl = "config/{$value['id']}/edit";
                                $delUrl = "config/{$value['id']}";
                                $redirect = "config";
                                $buttonGroup = "<div class='btn-group shadow-0 btn-xs'>
                                    <button data-url='{$editUrl}' class='btn btn-info shadow-0 edit-config'> <i class='fa fa-edit'></i> </button>
                                    <button data-url='{$delUrl}' data-action='delete' data-redirect='{$redirect}' class='btn btn-danger shadow-0 del-config'> <i class='feather icon-trash'></i> </button>
                                </div>";
                                $this->html($buttonGroup);

                            }
                        }
                    });
                }
            });
        }

        \Dcat\Admin\Form::dialog("修改配置")
            ->click(".edit-config")
            ->width('1200px')
            ->success('Dcat.reload()')->forceRefresh();
    }
}
